import { App3D } from './core/App3D.js';

// 初始化3D应用
const app = new App3D({
    antialias: true,
    alpha: true
});

// 启动应用
app.ready().then(() => {
    console.log('3D应用初始化完成');
    
    // 将app实例暴露到全局，方便调试
    window.app3D = app;
    
    // 添加调试方法到全局
    window.debugMeshes = () => app.debugAllSceneMeshes();
    window.hideAllMeshes = () => app.setAllMeshesVisible(false);
    window.showAllMeshes = () => app.setAllMeshesVisible(true);
    
    console.log('调试方法已添加到全局:');
    console.log('- window.debugMeshes() - 打印所有mesh信息');
    console.log('- window.hideAllMeshes() - 隐藏所有mesh');
    console.log('- window.showAllMeshes() - 显示所有mesh');
    console.log('- window.app3D - 访问主应用实例');
    
}).catch(error => {
    console.error('3D应用初始化失败:', error);
});

// 处理窗口大小变化
window.addEventListener('resize', () => {
    app.resize();
});

// 开发环境下添加性能监控
if (import.meta.env.DEV) {
    // 添加FPS监控
    let frameCount = 0;
    let lastTime = performance.now();
    
    function updatePerformanceInfo() {
        frameCount++;
        const currentTime = performance.now();
        
        if (currentTime - lastTime >= 1000) {
            const fps = Math.round((frameCount * 1000) / (currentTime - lastTime));
            document.getElementById('fps').textContent = fps;
            
            // 更新渲染统计
            const renderer = app.getRenderer();
            if (renderer && renderer.info) {
                document.getElementById('render-objects').textContent = renderer.info.render.triangles;
            }
            
            // 估算内存使用
            if (performance.memory) {
                const memoryMB = Math.round(performance.memory.usedJSHeapSize / 1048576);
                document.getElementById('memory').textContent = memoryMB;
            }
            
            frameCount = 0;
            lastTime = currentTime;
        }
        
        requestAnimationFrame(updatePerformanceInfo);
    }
    
    updatePerformanceInfo();
}
